package com.bone.title.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bone.com.service.ComLogService;
import com.bone.constant.Constant;
import com.bone.title.dao.TitleDAO;
import com.bone.title.dao.TitleSettingDAO;
import com.bone.title.po.Title;
import com.bone.title.po.TitleSetting;
import com.bone.title.service.TitleService;
import com.bone.vo.TitleVO;

public class TitleServiceImpl implements TitleService   {
	
	/**
	 * DAO.
	 */
	private TitleDAO titleDAO;
	/**
	 * 
	 */
	private TitleSettingDAO titleSettingDAO;
	/**
	 * 	
	 */
	private ComLogService comLogService;
 
	//--------title 标题 start ---------------
	 /**
     * 保存或者修改模块.	
     * @param module
     * @return
     */
	public boolean saveOrUpdateTitle(Title title,String operatorId){
		boolean isSuccess = false;
		String type ="";
		if(title.getTitleId()!=null && title.getTitleId() > 0){
			isSuccess = titleDAO.updateTitle(title);
			type = "UPDATE";
		}else{
			title.setCreateTime(new Date());
			title.setCreateUser(String.valueOf(operatorId));
			isSuccess = titleDAO.insertTitle(title);
			type = "SAVE";
		}
//		saveOrUpdateTitleSettingByTitle
		this.saveOrUpdateTitleSettingByTitle(title, operatorId, type);
		if("SAVE".equals(type)){
			comLogService.insertComlog("TITLE", title.getTitleId(), operatorId,"SAVE", "添加标题","添加标题","");
		}if("UPDATE".equals(type)){
			comLogService.insertComlog("TITLE", title.getTitleId(), operatorId,"UPDATE", "更新标题","更新标题","");
		}
		return isSuccess;
	} 
	/**
	 * 取符合查询条件的标题个数.
	 * @param para
	 * @return
	 */
	public Long queryTitleCountByParam(Map<String, Object> para){
		return titleDAO.queryTitleCountByParam(para);
	}
	/**
	 * 取符合查询条件的标题数据.
	 * @param para
	 * @return
	 */
	public List<Title> queryTitlePageByParam(Map<String, Object> para){
		return titleDAO.queryTitlePageByParam(para);
	}
	/**
	 * 取符合查询条件的标题数据.
	 * @param para
	 * @return
	 */
	public List<Title> queryTitleListByParam(Map<String, Object> para){
		return titleDAO.queryTitleListByParam(para);
	}
	/**
	 * 根据标题的ID取相应的标题信息.
	 * @param titleId
	 * @return
	 */
	public Title queryTitleByTitleId(Long titleId){
		return titleDAO.queryTitleByTitleId(titleId);
	}
	/**
	 * 根据标题的Id取标题的名字.
	 * @param titleId
	 * @return
	 */
	public String queryTitleNameByTitleId(Long titleId){
		Title title = titleDAO.queryTitleByTitleId(titleId);
		return title.getTitleName();
	}
	
	/**
	 * 根据标题的增加和修改保存标题菜单设置. 
	 * @param title
	 * @param operatorId
	 * @param operatorId 保存或者修改.
	 * @param level 菜单级别.
	 * @return
	 */
	private boolean saveOrUpdateTitleSettingByTitle(Title title,String operatorId,String type){
		TitleSetting ts = new TitleSetting();
		if("SAVE".equalsIgnoreCase(type)){
			ts.setTitleId(title.getTitleId());
		    ts.setSeq(Double.valueOf(title.getUpEvel()));
		    ts.setLevel(title.getTitleType());
		    ts.setStatus(title.getTitleStatus());
		    
//		    ts.setTitleSub(titleSub);
		}else if("UPDATE".equalsIgnoreCase(type)){
			ts = this.queryTitleSettingByTitleId(title.getTitleId());
			ts.setTitleId(title.getTitleId());
			ts.setSeq(Double.valueOf(title.getUpEvel()));
		    ts.setLevel(title.getTitleType());
		    ts.setStatus(title.getTitleStatus());
		}
		this.saveOrUpdateTitleSetting(ts, operatorId);
		return true;
	}
	
	/**
     * 保存或者修改 TitleSetting.	
     * @param TitleSetting
     * @return
     */
	public boolean saveOrUpdateTitleSetting(TitleSetting titleSetting,String operatorId){
		boolean isSuccess = false;
		String type ="";
		if(titleSetting.getSetId()!=null && titleSetting.getSetId() > 0){
			isSuccess = titleSettingDAO.updateTitleSetting(titleSetting);
			type = "UPDATE";
		}else{
			isSuccess = titleSettingDAO.insertTitleSetting(titleSetting);
			type = "SAVE";
		}
		if("SAVE".equals(type)){
			comLogService.insertComlog("TITLE", titleSetting.getSetId(), operatorId,"SAVE", "添加标题设置","添加标题设置","");
		}if("UPDATE".equals(type)){
			comLogService.insertComlog("TITLE", titleSetting.getSetId(), operatorId,"UPDATE", "更新标题设置","更新标题设置","");
		}
		return isSuccess;
	} 
	/**
	 * 取符合查询条件的TitleSetting个数.
	 * @param para
	 * @return
	 */
	public Long queryTitleSettingCountByParam(Map<String, Object> para){
		return titleSettingDAO.queryTitleSettingCountByParam(para);
	}
	/**
	 * 取符合查询条件的TitleSetting数据.
	 * @param para
	 * @return
	 */
	public List<TitleSetting> queryTitleSettingListByParam(Map<String, Object> para){
		return titleSettingDAO.queryTitleSettingListByParam(para);
	}
	/**
	 * 根据TitleSetting的ID取相应的TitleSetting信息.
	 * @param titleId
	 * @return
	 */
	public TitleSetting queryTitleBySetId(Long setId){
		return titleSettingDAO.queryTitleSettingBySetId(setId);
	}
	
	/**
	 * 根据TitleSetting的ID取相应的TitleSetting信息.
	 * @param titleId
	 * @return
	 */
	public TitleSetting queryTitleSettingByTitleId(Long titleId){
		return titleSettingDAO.queryTitleSettingByTitleId(titleId);
	}
	
	/**
	 * 根据上级的菜单ID取下级有效的菜单列表.
	 * @param OneTitleId
	 * @param status
	 * @return
	 */
	private TitleVO queryLowerTitleListByUpperTitleId(TitleVO titlevo,String titleIds,String status,String level,boolean key){
		if(key){
			String tIds[] = titleIds.split(",");
		    for(int i=0;i<tIds.length;i++){
		    	TitleVO tvo = new TitleVO();
		    	String titleId = tIds[i];
		    	TitleSetting ts = queryTitleSettingByTitleId(Long.valueOf(titleId));
				tvo.setTitleId(ts.getTitleId());
				tvo.setSeq(ts.getSeq());
				tvo.setSetId(ts.getSetId());
				tvo.setStatus(ts.getStatus());
				tvo.setLevel(ts.getLevel());
				tvo.setTitleName(queryTitleNameByTitleId(ts.getTitleId()));
				
				
				String titIds = ts.getTitleSub();
				if(titIds!=null){
					String ttIds[] = titIds.split(",");
					for (int ii = 0; ii < ttIds.length; ii++) {
						TitleVO ttvo = new TitleVO();
						String titId = ttIds[ii];
						TitleSetting titleset = queryTitleSettingByTitleId(Long.valueOf(titId));
						ttvo.setTitleId(titleset.getTitleId());
						ttvo.setSeq(titleset.getSeq());
						ttvo.setSetId(titleset.getSetId());
						ttvo.setStatus(titleset.getStatus());
						ttvo.setLevel(titleset.getLevel());
						ttvo.setTitleName(queryTitleNameByTitleId(titleset.getTitleId()));

						tvo.putTs(ttvo);
					}
				}
			    titlevo.putTs(tvo);
		    } 
		}
	 return	titlevo;
	}
	
	
	/**
	 * 组装菜单的显示方式.
	 * @param tsList
	 * @return
	 */
	public  List<TitleVO>  findMyTitleList(String level,String status) {
		
		List<TitleVO> tvoList = new ArrayList<TitleVO>(); 
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status",status);
		map.put("level", level);
		List<TitleSetting> onetsList = queryTitleSettingListByParam(map);
		
		if(onetsList!=null && onetsList.size()>0){
			for(int i = 0;i<onetsList.size();i++){
				TitleVO tvo = new TitleVO();
				TitleSetting ts = onetsList.get(i);
				tvo.setTitleId(ts.getTitleId());
				tvo.setSeq(ts.getSeq());
				tvo.setSetId(ts.getSetId());
				tvo.setStatus(ts.getStatus());
				tvo.setLevel(ts.getLevel());
				tvo.setTitleName(queryTitleNameByTitleId(ts.getTitleId()));
				String titleIds = ts.getTitleSub();
				if(titleIds!=null){
				tvo = queryLowerTitleListByUpperTitleId(tvo, titleIds, status, level, true);
				}
				tvoList.add(tvo);
			}
		} 
		return tvoList;
	}
		

	
	
	public TitleDAO getTitleDAO() {
		return titleDAO;
	}
	public void setTitleDAO(TitleDAO titleDAO) {
		this.titleDAO = titleDAO;
	}
	public TitleSettingDAO getTitleSettingDAO() {
		return titleSettingDAO;
	}
	public void setTitleSettingDAO(TitleSettingDAO titleSettingDAO) {
		this.titleSettingDAO = titleSettingDAO;
	}
	public ComLogService getComLogService() {
		return comLogService;
	}
	public void setComLogService(ComLogService comLogService) {
		this.comLogService = comLogService;
	}
	 

}
